<template>
  <div class="container tree-list-layout">
    <div class="box-left">
      <customTree
        ref="customTree"
        :data="treeData"
        @click="treeClick"
        @load="getTreeData"
        :checkable="false"
        :subHeight="202"
      />
    </div>
    <data-view
      propViewKey="certificate_hospital_business"
      :propIsTdesign="true"
      :propNoEditRow="true"
      :propBeforeClick="propBeforeClickDtl"
      class="box-right"
      :propAutoFetchData="false"
      ref="childDataView"
    />

    <!--预定义资质选择-->
    <tableDrawer
      ref="tableDrawerCert"
      loadForce
      refName="tableDrawerCert"
      viewKey="certificate_select_for_hpdefine"
      title="请选择预定义资质"
      :subtitle="cpSubtitle"
      :widthPer="50"
      :heightPx="-1"
      :widthPx="-1"
      :queryWrapper="cpCertQueryWrapper"
    />
  </div>
</template>

<script>
export default {
  help: '院方定义资质-列表',
  data() {
    return {
      treeData: [],
      selectedTreeNode: null
    }
  },
  computed:{
    cpCertQueryWrapper() {
      let queryWrapper = this.$newWrapper()
      if (this.selectedTreeNode) {
        queryWrapper.eq('businessTypeCode', this.selectedTreeNode.value)
      }
      return queryWrapper
    },
    cpSubtitle() {
      let subtitle = '<span class="span-msg">没有添加到院方资质定义的</span>'
      if (this.selectedTreeNode) {
        subtitle+= ',' + global.utils.getDictLabel(this.selectedTreeNode.value, global.constCommon.dictType.certBusinessType, '', true)
      }
      return subtitle
    }
  },
  mounted() {
    this.getTreeData()
    this.loadDtl()
  },
  methods: {
    // 加载明细数据
    loadDtl() {
      let queryWrapper = this.$newWrapper()
      if (this.selectedTreeNode) {
        queryWrapper.eq('businessTypeCode', this.selectedTreeNode.value)
      }
      this.$refs.childDataView.setQueryWrapper(queryWrapper)
    },
    // 列表添加按钮
    propBeforeClickDtl(button, formData , rowData, that) {
      //列表添加按钮 其他入库
      if (button.key == 'vb-add') {
        this.$refs.tableDrawerCert.show(this.certSelect, true)
        return false
      }
      return true;
    },
    certSelect(rows) {
      this.utilVue.loadShow(true)
      rows.forEach(row => {
        row.certificateId = row.id
        row.id = null
      })
      this.$ajax.put('/cert/certificate_hospital_businesss/batch_save', rows).then((resp) => {
        this.utilVue.loadHide()
        if (resp.code == 0) {
          this.utilMessage.success(resp.msg)
          this.loadDtl()
        }
      })
    },
    // 节点点击
    treeClick(data) {
      this.selectedTreeNode = data
      this.loadDtl()
    },
    // 加载数据
    getTreeData() {
      this.treeData = [
        {
          value: '',
          label: '全部资质类型',
        }
      ]

      this.$ajax.get('cert/certificates/listForHpdefineSelect').then(response => {
        console.log('response.data', response.data)
        if (response.code == 0 && this.treeData && this.treeData.length > 0) {
          let treeDataTmp = []
          response.data.forEach(item => {
            treeDataTmp.push({
              value: item.businessTypeCode,
              label: item.businessTypeLabel,
            })
          })
          this.treeData = [
            {
              value: '',
              label: '全部资质类型',
              children: treeDataTmp
            }
          ]
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
/*左树右列表布局*/
.tree-list-layout{
  .box-left{
    width: 250px !important;
  }
  .box-right{
    width: calc(100% - 250px) !important;
  }
}
</style>
